﻿<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WPFZ80MapEditor"
    xmlns:localconverters="clr-namespace:WPFZ80MapEditor.ValueConverters"
    x:Class="MapEditorControl"
    x:Name="MapEditorControl" Background="White">

    <UserControl.Resources>
        <localconverters:DoubleToZoomLevelConverter x:Key="convertZoomLevel2"/>
        <localconverters:GapConverter x:Key="GapConverter"/>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter1" />
        <local:LayerHitTestConverter x:Key="LayerHitTestConverter"/>
        <local:ActiveLayerConverter x:Key="ActiveLayerConverter"/>
        <local:MapPositionConverter x:Key="MapPositionConverter"/>

        <TransformGroup x:Key="MapCanvasRenderTransform">
            <ScaleTransform/>
            <TranslateTransform X="-60" Y="-180"/>
        </TransformGroup>

        <Style x:Key="LayerStyle" TargetType="{x:Type local:MapLayer}">
            <Setter Property="IsHitTestVisible" Value="False"/>
            <Style.Triggers>
                <DataTrigger Value="True">
                    <DataTrigger.Binding>
                        <MultiBinding Converter="{StaticResource ActiveLayerConverter}">
                            <Binding Path="DataContext.CurrentLayer" RelativeSource="{RelativeSource AncestorType={x:Type local:MapEditorControl}}"/>
                            <Binding Path="LayerType" RelativeSource="{x:Static RelativeSource.Self}"/>
                        </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter Property="IsHitTestVisible" Value="True"/>
                    <Setter Property="Active" Value="True"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Canvas x:Name="MapCanvas" Background="Gray" Panel.ZIndex="0" MouseMove="Background_MouseMove" MouseWheel="Background_MouseWheel" PreviewMouseRightButtonDown="Background_PreviewMouseRightButtonDown" PreviewMouseRightButtonUp="Background_PreviewMouseRightButtonUp">
            <Canvas x:Name="IntermediateMapCanvas" RenderTransform="{StaticResource MapCanvasRenderTransform}">
                <local:XLayerContainer 
                        x:Name="LayerContainer"
                        Maps="{Binding Maps}" DataContext="{Binding Scenario}" SelectedItem="{Binding DataContext.SelectedMap, ElementName=MapEditorControl, Mode=TwoWay}"
					    Gap="{Binding IsChecked, Converter={StaticResource GapConverter}, ElementName=PaddingMenuItem}"
					     MouseDoubleClick="Map_MouseDoubleClick">
                    <local:XLayerContainer.Style>
                        <Style TargetType="{x:Type local:XLayerContainer}">
                            <Setter Property="Selectable" Value="False"/>
                            <Style.Triggers>
                                <DataTrigger Value="True">
                                    <DataTrigger.Binding>
                                        <MultiBinding Converter="{StaticResource ActiveLayerConverter}">
                                            <Binding Path="DataContext.CurrentLayer" RelativeSource="{RelativeSource AncestorType={x:Type local:MapEditorControl}}"/>
                                            <Binding Source="{x:Static local:LayerType.MapsetLayer}"/>
                                        </MultiBinding>
                                    </DataTrigger.Binding>
                                    <Setter Property="Selectable" Value="True"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </local:XLayerContainer.Style>
                    <local:XLayerContainer.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <local:XMapView Panel.ZIndex="1"
										    SourceTile="{Binding DataContext.SelectedTile, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:MapEditorControl}}}">
                                    <local:XMapView.Style>
                                        <Style TargetType="local:XMapView" BasedOn="{StaticResource LayerStyle}">
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding Path=DataContext.CurrentLayer, RelativeSource={RelativeSource AncestorType=local:MapEditorControl}}"
                                                 Value="{x:Static local:LayerType.MapsetLayer}">
                                                    <DataTrigger.Setters>
                                                        <Setter Property="ShowNewMaps" Value="True"/>
                                                    </DataTrigger.Setters>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=DataContext.CurrentLayer, RelativeSource={RelativeSource AncestorType=local:MapEditorControl}}"
                                                 Value="{x:Static local:LayerType.MapLayer}">
                                                    <DataTrigger.Setters>
                                                        <Setter Property="ShowCollisions" Value="True"/>
                                                    </DataTrigger.Setters>
                                                </DataTrigger>
                                                <DataTrigger Binding="{Binding Path=DataContext.CurrentLayer, RelativeSource={RelativeSource AncestorType=local:MapEditorControl}}"
                                                 Value="{x:Static local:LayerType.TilegroupLayer}">
                                                    <DataTrigger.Setters>
                                                        <Setter Property="ShowCollisions" Value="True"/>
                                                    </DataTrigger.Setters>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </local:XMapView.Style>
                                </local:XMapView>

                                <local:TilegroupLayer Panel.ZIndex="2" Style="{StaticResource LayerStyle}"
                                                      SourceTile="{Binding DataContext.SelectedTile, RelativeSource={RelativeSource AncestorType=local:MapEditorControl}}">
                                </local:TilegroupLayer>
                                <local:MiscLayer Panel.ZIndex="3" Style="{StaticResource LayerStyle}"/>
                                
                                <local:XXObjectLayer Panel.ZIndex="4" Style="{StaticResource LayerStyle}" ObjectCollection="{Binding ZObjects}" ObjectType="{x:Type local:ZObject}" ObjectEffect="{StaticResource ObjectTintEffect}"/>
                                <local:XXObjectLayer Panel.ZIndex="5" Style="{StaticResource LayerStyle}" ObjectCollection="{Binding ZEnemies}" ObjectType="{x:Type local:ZEnemy}" ObjectEffect="{StaticResource EnemyTintEffect}"/>
                                                                
                                <local:TestLayer Panel.ZIndex="6" Style="{StaticResource LayerStyle}" GameModel="{Binding DataContext.GameModel, RelativeSource={RelativeSource AncestorType=local:MapEditorControl}}"/>
                            </Grid>
                        </DataTemplate>
                    </local:XLayerContainer.ItemTemplate>
                </local:XLayerContainer>

                <local:GameMapView x:Name="TestView" Panel.ZIndex="1" Width="256" Height="256" DataContext="{Binding GameModel}" Visibility="{Binding IsInitialized, Converter={StaticResource BooleanToVisibilityConverter1}, FallbackValue=Hidden}"
                        Canvas.Left="{Binding Path=SourceMap.X, Converter={StaticResource MapPositionConverter}, ConverterParameter=8}"
                        Canvas.Top="{Binding Path=SourceMap.Y, Converter={StaticResource MapPositionConverter}, ConverterParameter=8}">

                </local:GameMapView>
            </Canvas>
        </Canvas>
    </Grid>
</UserControl>
